# APKBUILD based on linux-vanilla aport. Changes:
# - disabled module installation
# - add !check !tracedeps
# - package: just install zimage and kernel.release, because the kernel config
#	does not generate modules or dtb files
# - do not create -dev subpackage (makes no sense without module support)
#
# Kernel config based on: arch/arm/configs/lineageos_mako_defconfig
# Changes:
# - enable devtmpfs (needed for udev -> touch support in weston)

_vendor=nokia
_flavor=nokia-rx51
_hash="7b6fbfd899b4133e4f789843e5967beb139ec4d2"
_config="config-${_flavor}.armhf"

pkgname=linux-${_flavor}
pkgver=4.6
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=2
arch="armhf"
pkgdesc="Mainline Linux for the Nokia N900"
url="https://github.com/pali/linux-n900"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev uboot-tools"
options="!strip !check !tracedeps"
install=
source="
	$pkgname-$_hash.tar.gz::https://github.com/pali/linux-n900/archive/${_hash}.tar.gz
	$_config
"
subpackages=""
license="GPL2"

_abi_release=${pkgver}
_carch="arm"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/linux-n900-${_hash}"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config || return 1
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig
}



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build || return 1
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS
	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine" \
		CFLAGS_MODULE=-fno-pic \
		zImage modules omap3-n900.dtb \
		|| return 1
}

package() {
	cd "$srcdir/build/arch/arm/boot"
	
	cat zImage dts/omap3-n900.dtb > zImage-dtb
	mkimage -A arm -O linux -T kernel -C none -a 80008000 -e 80008000 -n postmarketos -d zImage-dtb uImage

	install -Dm644 "$srcdir/build/arch/arm/boot/uImage" \
		"$pkgdir/boot/uImage"

	install -Dm644 "$srcdir/build/arch/arm/boot/zImage-dtb" \
		"$pkgdir/boot/vmlinuz-$_flavor"

	install -Dm644 "$srcdir/build/arch/arm/boot/dts/omap3-n900.dtb" \
		"$pkgdir/boot/omap3-n900.dtb"

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"

	cd "$srcdir"/build
	make -j1 modules_install firmware_install \
		ARCH="$_carch" \
		INSTALL_MOD_PATH="$pkgdir" \
		|| return 1
}

sha512sums="ad1288233893ce3de9c81f4672f4f2439c8d6665a0a0289f5cbe826fd08c69292de02a2fd9856dcd6d91c7f6930273319e1888bf0a146aee3a4e1ff50f6f7b19  linux-nokia-rx51-7b6fbfd899b4133e4f789843e5967beb139ec4d2.tar.gz
6b9ef5266bf95314b76c6973036b8e66cfa3b27efee84524f8c9a4316cbc29e289b1d56c99d8bcdf5516bdc743ab1a2bea4b277d3cff88d0e07de4b514d65e77  config-nokia-rx51.armhf"
